package cn.taketoday.live.handler;

import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;

import cn.taketoday.live.WebSocketMessage;
import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.config.settings.ThankGiftSetConf;
import cn.taketoday.live.data.Gift;
import cn.taketoday.live.data.enums.ShieldGift;
import cn.taketoday.live.data.enums.ShieldMessage;
import cn.taketoday.live.tools.ParseIndentityTools;
import cn.taketoday.live.tools.ParseSetStatusTools;
import cn.taketoday.live.tools.ShieldGiftTools;
import cn.taketoday.live.utils.JodaTimeUtils;

/**
 * @author TODAY 2021/5/17 22:59
 */
public class GiftCommandHandler implements CommandHandler {

  @Override
  public void handle(CommandContext context) throws Exception {
    JSONObject jsonObject = context.getOriginal();
    Gift gift = null;
    final StringBuilder stringBuilder = new StringBuilder(32);
    if (context.contains(ShieldMessage.is_gift)) {
      jsonObject = JSONObject.parseObject(jsonObject.getString("data"));
      gift = Gift.getGift(jsonObject.getInteger("giftId"), jsonObject.getShort("giftType"),
                          jsonObject.getString("giftName"), jsonObject.getInteger("num"),
                          jsonObject.getString("uname"), jsonObject.getString("face"),
                          jsonObject.getShort("guard_level"), jsonObject.getLong("uid"),
                          jsonObject.getLong("timestamp"), jsonObject.getString("action"),
                          jsonObject.getInteger("price"),
                          ParseIndentityTools.parseCoin_type(jsonObject.getString("coin_type")),
                          jsonObject.getLong("total_coin"));
//							giftFile = new GiftFile(jsonObject.getInteger("giftId"), jsonObject.getString("giftName"),
//									jsonObject.getInteger("price"), jsonObject.getString("coin_type"));
//							GiftFileTools.addGiftToFile(giftFile.toJson());
      stringBuilder.append(JodaTimeUtils.formatDateTime(gift.getTimestamp() * 1000));
      stringBuilder.append(":收到道具:");
      stringBuilder.append(gift.getUname());
      stringBuilder.append(" ");
      stringBuilder.append(gift.getAction());
      stringBuilder.append("的:");
      stringBuilder.append(gift.getGiftName());
      stringBuilder.append(" x ");
      stringBuilder.append(gift.getNum());
      //控制台打印

      if (context.contains(ShieldMessage.is_cmd)) {
        System.out.println(stringBuilder.toString());
      }
      context.sendMessage(WebSocketMessage.of("gift", gift));
      if (GlobalData.logThread != null && !GlobalData.logThread.FLAG) {
        GlobalData.logString.add(stringBuilder.toString());
        synchronized(GlobalData.logThread) {
          GlobalData.logThread.notify();
        }
      }
      stringBuilder.delete(0, stringBuilder.length());
    }
    else {
      if (!GlobalData.parsethankGiftThread.TFLAG) {
        jsonObject = JSONObject.parseObject(jsonObject.getString("data"));
        gift = Gift.getGift(jsonObject.getInteger("giftId"), jsonObject.getShort("giftType"),
                            jsonObject.getString("giftName"), jsonObject.getInteger("num"),
                            jsonObject.getString("uname"), jsonObject.getString("face"),
                            jsonObject.getShort("guard_level"), jsonObject.getLong("uid"),
                            jsonObject.getLong("timestamp"), jsonObject.getString("action"),
                            jsonObject.getInteger("price"),
                            ParseIndentityTools.parseCoin_type(jsonObject.getString("coin_type")),
                            jsonObject.getLong("total_coin"));
      }
    }

    // 感谢礼物处理
    if (gift != null && context.contains(ShieldMessage.is_giftThank)) {
      final ThankGiftSetConf thankGiftSetConf = context.getMessageHandler().getThankGiftSetConf();
      if (ParseSetStatusTools.getGiftShieldStatus(thankGiftSetConf.getShield_status()) != ShieldGift.CUSTOM_RULE) {
        final ShieldGift giftShieldStatus = ParseSetStatusTools.getGiftShieldStatus(thankGiftSetConf.getShield_status());
        gift = ShieldGiftTools.shieldGift(gift, giftShieldStatus, thankGiftSetConf.getGiftStrings(), null);
      }
      if (gift != null) {
        if (!StringUtils.isEmpty(GlobalData.SHIELDGIFTNAME)) {
          if (gift.getGiftName().equals(GlobalData.SHIELDGIFTNAME)) {
            gift = null;
          }
        }
      }

      context.getMessageHandler().parseGiftSetting(gift);
    }
  }

}
